package com.lagou.util;

import com.google.gson.Gson;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResultUtil {

    public static void writeJson(HttpServletResponse response, Object result) throws Exception {
        response.setContentType("text/json;charset=utf-8");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        PrintWriter pw = null;
        Gson gson = new Gson();
        try {
            pw = response.getWriter();
            pw.write(gson.toJson(result));
            pw.flush();
        } catch (Exception e) {
//            logger.info("输出JSON数据异常", e);
            throw new IOException();
        }finally{
            if(pw!=null){
                pw.close();
            }
        }
    }
}
